package com.aaa.service.impl;

import com.aaa.entity.Video;
import com.aaa.mapper.VideoMapper;
import com.aaa.service.IVideoService;
import com.aaa.util.ImageGif;
import com.aaa.util.secl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import org.assertj.core.internal.Integers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.time.LocalTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Bean
 * @since 2021-09-03
 */
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements IVideoService {

    @Autowired
    VideoMapper videoMapper;
    @Autowired
    com.aaa.util.secl secl;
    @Autowired
    ImageGif imageGif;

    public List<Video> userid(Integer uid,Integer id){
        return videoMapper.userid(uid,id);
    }



    public List<Map<String,Object>> likevideo(Integer uid){
        return videoMapper.likevideo(uid);
    }


    public List<Map<String,Object>> attenvideo(Integer uid){
        return videoMapper.attenvideo(uid);
    }



    public boolean add(MultipartFile video, Integer uid,  String introduce, Integer state) throws Exception {
        String whole = secl.whole(LocalTime.now());
        String filename = video.getOriginalFilename();
        String url="D:/idea/queick/"+uid+"/video/"+whole+"/"+filename;
        if(!filename.equals("")){
            String substring = video.getContentType().substring(0, video.getContentType().indexOf("/"));
            if (substring.equals("video")){
                File file = new File(url);
                if (!file.exists()) {
                    file.mkdirs();
                }
                    //另存为
                video.transferTo(file);
            }
            String cover_url = imageGif.fetchFrame(url, ".jpg", 5);
            String cover_gif_url = imageGif.buildGif(url, 1, 10, 3, 30);
            System.out.println(cover_url);
            System.out.println(cover_gif_url);
            Integer add = videoMapper.add(uid, url, cover_url, cover_gif_url, introduce,state);
            if (add==1){
                return true;
            }
        }
        return  false;
    }

    public List<Map<String,Object>> recomment(Integer uid,Integer page){
        PageHelper.startPage(page,8);
        return videoMapper.recomment(uid);
    }


}
